$(function () {
    init_shop_list();
    add_shop();
    to_spare_shore();
});

function shop_detail() {
    $('.shop-clicker').click(function () {
        var target_id = $(this).attr('uid');
        window.location.href=`/shop-detail?id=${target_id}`;
    })
}

function add_shop() {
    $('#add-shop').click(() => {
        $('#add-shop').attr('class', 'btn btn-success');
        $('#add-shop').text('正在跳转..');
        setTimeout(function () {
            window.location.href = '/page/shoper-addproduct.html';
        }, 1500);
    });
}

function to_spare_shore() {
    $('#to-spare-store').click(() => {
        window.location.href = '/page/shoper-spare.html';
    })
}

function init_shop_list() {
    $.ajax({
        url: '/products-list',
        type: 'GET',
        dataType: 'JSON',
        success: function (data) {
            if (data.status) {
                var result_list = new Array(data.result);
                result_list.forEach((value, _idx, _arr) => {
                    value.forEach((sub_value, _sub_idx, _sub_arr) => {
                        $s_body = `
                            <div class="col-lg-4 col-md-6 col-sm-12 pb-1 shop-clicker" id="shop_${sub_value['id']}" uid="${sub_value['id']}">
                                <div class="card product-item border-0 mb-4">
                                    <div class="card-header product-img position-relative overflow-hidden bg-transparent border p-0">
                                        <img class="img-fluid w-100" src="${sub_value['image']}" alt="">
                                    </div>
                                    <div class="card-body border-left border-right text-center p-0 pt-4 pb-3">
                                        <h6 class="text-truncate mb-3">${sub_value['name']}</h6>
                                    </div>
                                    <div class="card-footer d-flex justify-content-between bg-light border">
                                    </div>
                                </div>
                            </div>
                        `;
                        $('#shop-body').append($s_body);
                    });
                });
                shop_detail();
            } else {
                alert('获取商品数据失败，请检查后台重试');
            }
        }
    });
}